//package org.example.test.websoket;
//import jakarta.websocket.server.ServerEndpoint;
//import org.springframework.ai.ollama.OllamaChatClient;
//import org.springframework.ai.chat.messages.ChatMessage;
//import org.springframework.ai.chat.prompt.Prompt;
//import org.springframework.stereotype.Component;
//
//import javax.websocket.*;
//import javax.websocket.server.ServerEndpoint;
//import java.io.IOException;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
//
//@ServerEndpoint("/chat")
//@Component
//public class WebSocketChatServer {
//
//    private static OllamaChatClient ollamaChatClient;
//    private static final ExecutorService executorService = Executors.newFixedThreadPool(10);
//
//    // 初始化 Spring AI Ollama 客户端
//    public static void setOllamaChatClient(OllamaChatClient client) {
//        ollamaChatClient = client;
//    }
//
//    @OnOpen
//    public void onOpen(Session session) {
//        System.out.println("WebSocket 连接已建立：" + session.getId());
//    }
//
//    @OnMessage
//    public void onMessage(String message, Session session) {
//        System.out.println("收到消息: " + message);
//
//        executorService.submit(() -> {
//            try {
//                Prompt prompt = new Prompt(new ChatMessage("user", message));
//                ollamaChatClient.stream(prompt).forEach(response -> {
//                    try {
//                        session.getBasicRemote().sendText(response.getGeneration().getText());
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                });
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        });
//    }
//
//    @OnClose
//    public void onClose(Session session) {
//        System.out.println("WebSocket 连接关闭：" + session.getId());
//    }
//
//    @OnError
//    public void onError(Session session, Throwable throwable) {
//        System.err.println("WebSocket 错误：" + throwable.getMessage());
//    }
//}
